package com.jeff.tianti.controller.channel;

import com.jeff.tianti.cms.dto.ColumnInfoQueryDTO;
import com.jeff.tianti.cms.dto.ConstitutionQueryDTO;
import com.jeff.tianti.cms.entity.ColumnInfo;
import com.jeff.tianti.cms.entity.Constitution;
import com.jeff.tianti.cms.service.ColumnInfoService;
import com.jeff.tianti.cms.service.ConstitutionService;
import com.jeff.tianti.common.ColumnInfoCom;
import com.jeff.tianti.common.dto.AjaxResult;
import com.jeff.tianti.utils.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 协会章程
 * @desc ConstitutionController
 * @date 2019-07-11
 */
@RestController
@RequestMapping("/channel/constitution")
@CrossOrigin(maxAge = 3600)
public class ConstitutionController {

    @Autowired
    private ConstitutionService constitutionService;
    @Autowired
    private ColumnInfoService columnInfoService;
    @Autowired
    private ColumnInfoCom columnInfoCom;


    @RequestMapping(value = "/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public AjaxResult list() {
        List<ColumnInfo> columnInfos = columnInfoCom.getList(Constants.CODE_JSPD_JSGX_XHZC);
        Map result = new HashMap<>();
        for (int i = 0; i < columnInfos.size(); i++) {
            ColumnInfo columnInfo = columnInfos.get(i);
            String columnInfoId = columnInfo.getId();
            String name = columnInfo.getName();
            ConstitutionQueryDTO constitutionQueryDTO = new ConstitutionQueryDTO();
            constitutionQueryDTO.setColumnId(columnInfoId);
            List<Map> constitutionList = constitutionService.queryList(constitutionQueryDTO);
            result.put("name", name);
            result.put("content", constitutionList);
        }
        return AjaxResult.setSuccessData(result);
    }
}